package com.inspur.system.service.impl;

import com.inspur.common.core.domain.entity.SysUser;
import com.inspur.common.utils.ListUtils;
import com.inspur.common.utils.SecurityUtils;
import com.inspur.common.utils.StringUtils;
import com.inspur.system.domain.PmPro;
import com.inspur.system.dto.PmWorkItemDTO;
import com.inspur.system.dto.ProNumDTO;
import com.inspur.system.mapper.PmProMapper;
import com.inspur.system.mapper.PmWorkItemMapper;
import com.inspur.system.mapper.SysUserMapper;
import com.inspur.system.service.IPmWorkItemService;
import com.inspur.system.utils.PmWorkItemUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 【工作项】Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-04-09
 */
@Service
public class PmWorkItemServiceImpl implements IPmWorkItemService
{
    @Autowired
    private PmWorkItemMapper pmWorkItemMapper;

    @Autowired
    private PmProMapper pmProMapper;

    @Autowired
    private SysUserMapper userMapper;

    /**
     * 查询【工作项】
     * 
     * @param cPmNo 【工作项】主键
     * @return 【工作项】
     */
    @Override
    public PmWorkItemDTO selectPmworkItemByCPmNo(Long cPmNo)
    {
        return pmWorkItemMapper.selectPmWorkItemByCPmNo(cPmNo);
    }

    /**
     * 查询【工作项】列表
     * 
     * @param pmWorkItem 【工作项】
     * @return 【工作项】
     */
    @Override
    public List<PmWorkItemDTO> selectPmworkItemList(PmWorkItemDTO pmWorkItem)
    {
        List<PmWorkItemDTO> pmWorkItemDTOList = new ArrayList<>();
        String cPmType = pmWorkItem.getcPmType();

        List<PmWorkItemDTO> pmWorkItemProList = pmWorkItemMapper.selectPmProList(pmWorkItem.getcProNo());
        for (PmWorkItemDTO pmWorkItemPro : pmWorkItemProList) {
            List<PmWorkItemDTO> pmWorkItemDemList = pmWorkItemMapper.selectPmDemList(pmWorkItemPro);
            List<PmWorkItemDTO> pmWorkItemFlawList = pmWorkItemMapper.selectPmFlawList(pmWorkItemPro);
            List<PmWorkItemDTO> pmWorkItemTaskList = pmWorkItemMapper.selectPmTaskList(pmWorkItemPro);

            if (StringUtils.isNotEmpty(cPmType)) {
                if ("11".equals(cPmType)) {
                    ListUtils.listAddAll(pmWorkItemDTOList,pmWorkItemDemList);
                } else if ("12".equals(cPmType)) {
                    ListUtils.listAddAll(pmWorkItemDTOList,pmWorkItemFlawList);
                } else if ("13".equals(cPmType)) {
                    ListUtils.listAddAll(pmWorkItemDTOList,pmWorkItemTaskList);
                }
            } else {
                ListUtils.listAddAll(pmWorkItemDTOList,pmWorkItemDemList);
                ListUtils.listAddAll(pmWorkItemDTOList,pmWorkItemFlawList);
                ListUtils.listAddAll(pmWorkItemDTOList,pmWorkItemTaskList);
            }
        }
        for (PmWorkItemDTO pmWorkItemDTO : pmWorkItemDTOList) {
            Double cEstWork = pmWorkItemDTO.getcEstWork(); // 预估工时
            Double cRemWork = pmWorkItemDTO.getcRemWork(); // 剩余工时
            String cEstRemWork = cEstWork + "h/" + cRemWork + "h";
            pmWorkItemDTO.setcEstRemWork(cEstRemWork);
            switch (pmWorkItemDTO.getcPmNo().substring(0, 2)) {
                case "10":
                    pmWorkItemDTO.setcPmType("10");
                    break;
                case "11":
                    pmWorkItemDTO.setcPmType("11");
                    break;
                case "12":
                    pmWorkItemDTO.setcPmType("12");
                    break;
                case "13":
                    pmWorkItemDTO.setcPmType("13");
                    break;
            }
            if (StringUtils.isNotEmpty(pmWorkItemDTO.getcProNo())) {
                PmPro pmPro = pmProMapper.selectPmProName(pmWorkItemDTO.getcProNo()).get(0);
                pmWorkItemDTO.setcProNme(pmPro.getcProNme());
            }
        }
        SysUser user = userMapper.selectUserById(SecurityUtils.getUserId());
        return PmWorkItemUtils.queryType(pmWorkItemDTOList,pmWorkItem.getcType(),user);
    }

    @Override
    public ProNumDTO selectPmworkItemNum(PmWorkItemDTO pmWorkItem)
    {
        return pmWorkItemMapper.selectPmworkItemNum(pmWorkItem);
    }

    /**
     * 批量删除【工作项】
     * 
     * @param cPmNos 需要删除的【工作项】主键
     * @return 结果
     */
    @Override
    public int deletePmworkItemByCPmNos(Long[] cPmNos)
    {
        return pmWorkItemMapper.deletePmworkItemByCPmNos(cPmNos);
    }

    /**
     * 删除【工作项】信息
     * 
     * @param cPmNo 【工作项】主键
     * @return 结果
     */
    @Override
    public int deletePmworkItemByCPmNo(Long cPmNo)
    {
        return pmWorkItemMapper.deletePmworkItemByCPmNo(cPmNo);
    }

    /**
     * 关联【工作项】
     * @return 结果
     */
    @Override
    public int demPmworkItemByCPmNos(String cPmNo,String cWork)
    {
        String cPmNo1 = cPmNo.substring(0,2);
        PmWorkItemDTO pmWorkItem = new PmWorkItemDTO();
        pmWorkItem.setcPmNo(cPmNo);
        pmWorkItem.setcWork(cWork);
        if ("10".equals(cPmNo1)) {
            pmWorkItem.setcTable("pm_pro");
            pmWorkItem.setcNo("C_PRO_NO");
        } else if ("11".equals(cPmNo1)) {
            pmWorkItem.setcTable("pm_dem");
            pmWorkItem.setcNo("C_DEM_NO");
        } else if ("12".equals(cPmNo1)) {
            pmWorkItem.setcTable("pm_flaw");
            pmWorkItem.setcNo("C_FLAW_NO");
        } else if ("13".equals(cPmNo1)) {
            pmWorkItem.setcTable("pm_task");
            pmWorkItem.setcNo("C_TASK_NO");
        }
        return pmWorkItemMapper.demPmworkItemByCPmNos(pmWorkItem);
    }

}
